<?php
/**
 * 临时会话
 * @author yangdong
 *
 */
namespace Session\Controller;
use Admin\Controller\CommonController;
use Session\Model\SessionModel;
class SessionController extends CommonController {
	private $session_db;
    private $push_db;
	function _initialize(){
		parent::_initialize();
		$this->session_db = new SessionModel();
	}
	/**
	 * 会话列表
	 * @param number $page
	 * @param number $rows
	 * @param unknown $search
	 */
	function index($page=1, $rows=10, $search = array()){
		if (IS_POST) {
			$map   = array();
			if ($search) {
				if ($search['name']){
					$map['name'] = array('like','%'.$search['name'].'%');
				}
			}
			$limit = ($page - 1) * $rows . "," . $rows;
			$total = $this->session_db->where($map)->count();
			$list  = $total ? $this->session_db->public_list(0, $map, $limit) : array();
			foreach ($list as $key=>$value){
				//格式化时间
				$list[$key]['createtime'] = date('Y-m-d H:i', $value['createtime']);
                $list[$key]['sid'] = $value['id'];
                switch ($value['status']){
                    case 0:
                        $list[$key]['status_text']='<font color="green">待审核</font>';
                        break;
                    case 1:
                        $list[$key]['status_text']='通过审核';
                        break;
                    case 2:
                        $list[$key]['status_text']='<font color="#8b0000">未通过审核</font>';
                        break;
                }
			}
			$this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
		}else {
    			$this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
			$this->display('list');
		}
	}

    /**
     * 待审核列表
     * @param int $page
     * @param int $rows
     * @param array $search
     */
	function check_index($page=1, $rows=10, $search = array()){
        if (IS_POST) {
            $map   = array();
            if ($search) {
                if ($search['name']){
                    $map['name'] = array('like','%'.$search['name'].'%');
                }
            }
            $map['status']=array('eq',0);
            $limit = ($page - 1) * $rows . "," . $rows;
            $total = $this->session_db->where($map)->count();
            $list  = $total ? $this->session_db->public_list(0, $map, $limit) : array();
            foreach ($list as $key=>$value){
                //格式化时间
                $list[$key]['createtime'] = date('Y-m-d H:i', $value['createtime']);

                switch ($value['status']){
                    case 0:
                        $list[$key]['status_text']='<font color="green">待审核</font>';
                        break;
                    case 1:
                        $list[$key]['status_text']='<font color="#08c">通过审核</font>';
                        break;
                    case 2:
                        $list[$key]['status_text']='<font color="#8b0000">未通过审核</font>';
                        break;
                }
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('check_list');
        }
    }

    /**
     * 审核通过
     * @param int $id
     */
    function statusPass($id=0){
        $return = $this->session_db->statusPass(I('uid'),I('id'));
        if ($return['code']) {
            $this->error($return['msg']);
        }else {
            $this->success($return['msg']);
        }
    }

    /**
     * 审核不通过
     * @param int $id
     */
    function statusNoPass($id=0){
        $return = $this->session_db->statusNoPass(I('uid'),I('id'));
        if ($return['code']) {
            $this->error($return['msg']);
        }else {
            $this->success($return['msg']);
        }
    }
	/**
	 * 编辑
	 * @param number $id
	 */
	function edit($id=0){
		if (IS_POST) {
			$return = $this->session_db->editSession(I('uid'), $id);
			if ($return['code']) {
				$this->error($return['msg']);
			}else {
				$this->success('修改成功');
			}
		}else {
			$this->info = $this->session_db->public_list(0, array('id'=>$id), 1);
			$this->rand = I('rand');
			$this->display('edit');
		}
	}
	/**
	 * 删除
	 * @param number $id
	 */
	function delete($id=0){
		$return = $this->session_db->deleteSession(I('uid'), $id);
		if ($return['code']) {
			$this->error($return['msg']);
		}else {
			$this->success($return['msg']);
		}
	}

    /**
     * 群聊推送设置
     * @param int $page
     * @param int $rows
     * @param array $search
     */
	function push($page=1, $rows=10, $search = array()){
        if (IS_POST) {
            $map   = array();
            $limit = ($page - 1) * $rows . "," . $rows;
            $total = M('push')->where($map)->count();
            $list  = $total ? M('push')->where($map)->limit($limit)->order('create_time desc')->select() : array();
            foreach ($list as $key=>$value){
                //格式化时间
                $list[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('push');
        }
    }
    /**
     * 添加推送关键字
     */
    function addPush(){
        $return = $this->session_db->addPush(I('keywords'));
        if ($return['code']) {
            $this->error($return['msg']);
        }else {
            $this->success($return['msg']);
        }
    }

    /**
     * 删除推送关键字
     */
    function deletePush(){
        $return = $this->session_db->deletePush(I('id'));
        if ($return['code']) {
            $this->error($return['msg']);
        }else {
            $this->success($return['msg']);
        }
    }

}